import { udp } from '@cscec/udp-ui';
import type { IDetailPage } from '../interface';

/**
 * 内置页面控制器
 */
export default function initDetailController(page: IDetailPage) {
  udp.AllReady(
    (editPage: IDetailPage, { useClick }) => {
      const { busType } = editPage;
      // 保存
      useClick(
        async () => {
          const key = await editPage.saveData();
          if (key !== false) {
            await udp.close();
          }
        },
        `${busType}_toolbar.save`,
        { loading: '正在保存' },
      );

      // 引用来源
      useClick(async () => {
        await editPage.quoteSrc();
      }, `${busType}_toolbar.quoteSrc`);

      // 送审
      useClick(async () => {
        const [success] = await editPage.startFlow(editPage.getMainForm());
        if (success) {
          await udp.close();
        }
      }, `${busType}_toolbar.check`);

      // 附件
      useClick(async () => {
        await editPage.openAttachment();
      }, `${busType}_toolbar.attachment`);

      // 返回
      useClick(async () => {
        await udp.close(true);
      }, `${busType}_toolbar.back`);
    },
    null,
    { delay: false }
  );
}
